XBtnDps={};
local TempDps={["Crit"]=false,["CurTime"]=nil,["DPS"]=0,["New"]=0};

function XBarDps_OnLoad(this)
	this:RegisterEvent("VARIABLES_LOADED");
	SaveVariables("XBtnDps");
end

XBarDps_OnUpdate=function()
	if not (TempDps["CurTime"]==nil) then
		if (GetTime()-TempDps["CurTime"]>10) then
			TempDps["Active"]=false;
			TempDps["DPS"]=0;
			TempDps["CurTime"]=nil;
		end
	end
end
 
function XBarDps_OnEvent(this,event)
-- ChatFrame1:AddMessage("XBar-Dps : "..event);
	if (event=="VARIABLES_LOADED") then
		this:RegisterEvent("UNIT_HEALTH");
		this:RegisterEvent("COMBATMETER_DAMAGE"); -- arg=_source,_target,_skill,_type,_damage
		this:RegisterEvent("COMBATMETER_HEAL"); -- arg=_source,_target,_skill,_type,_heal
	end
	if (event=="UNIT_HEALTH") then
		if (UnitHealth("target")==0 and TempDps["New"]==0) then
			TempDps["New"]=1;
			if not (XBtnDps["kills"]) then XBtnDps["kills"]=1;
			else XBtnDps["kills"]=XBtnDps["kills"]+1; end
		end
	end
	if (event=="COMBATMETER_DAMAGE") then
		TempDps["New"]=0;
		local Name=UnitName("player");
		local Class=UnitClass("player");
		if not (XBtnDps[Name]) then XBtnDps[Name]={}; end
		if not (XBtnDps[Name][Class]) then XBtnDps[Name][Class]={}; end
		if (_source~=UnitName("player")) then return; end
		if not (TempDps["Active"]) then
				TempDps["Active"]=true;
				TempDps["StartTime"]=(GetTime()-1);
				TempDps["TotalDmg"]=0;
				TempDps["DPS"]=0;
		end
		TempDps["CurTime"]=GetTime();
		TempDps["TotalTime"]=(TempDps["CurTime"]-TempDps["StartTime"]) ;
		TempDps["TotalDmg"]=TempDps["TotalDmg"]+_damage;
		TempDps["DPS"]=(math.floor(TempDps["TotalDmg"]/TempDps["TotalTime"]));
		TempDps["Crit"]=false;
		if not (XBtnDps[Name][Class][_skill]) then
			XBtnDps[Name][Class][_skill]={TDam=0,TCrt=0,BDam=0,BCrit=0,Avg=0,nHits=0,nCrits=0,nMiss=0,nParry=0,nDodge=0};
		end
		if (_type=="DOUBLE" or _type=="CRITICAL" or _type=="CRITIAL" or _type=="TRIPLE") then
			TempDps["Crit"]=true;
			XBtnDps[Name][Class][_skill].nCrits=XBtnDps[Name][Class][_skill].nCrits+1;
			XBtnDps[Name][Class][_skill].nHits=XBtnDps[Name][Class][_skill].nHits+1;
		elseif (_type=="HALF") then
			XBtnDps[Name][Class][_skill].nParry=XBtnDps[Name][Class][_skill].nParry+1;
			XBtnDps[Name][Class][_skill].nHits=XBtnDps[Name][Class][_skill].nHits+1;
		elseif (_type=="DODGE") then
			XBtnDps[Name][Class][_skill].nDodge=XBtnDps[Name][Class][_skill].nDodge+1;
		elseif (_type=="MISS") then
			XBtnDps[Name][Class][_skill].nMiss=XBtnDps[Name][Class][_skill].nMiss+1;
		elseif (_type=="NORMAL") then
			XBtnDps[Name][Class][_skill].nHits=XBtnDps[Name][Class][_skill].nHits+1;
		elseif (_type~="NORMAL") then
			XBtnDps[Name][Class][_skill].nHits=XBtnDps[Name][Class][_skill].nHits+1;
		end
		if (TempDps["Crit"]) then
			XBtnDps[Name][Class][_skill].TCrt=XBtnDps[Name][Class][_skill].TCrt+_damage;
			if (_damage>XBtnDps[Name][Class][_skill].BCrit) then XBtnDps[Name][Class][_skill].BCrit=_damage; end
		else 
			XBtnDps[Name][Class][_skill].TDam=XBtnDps[Name][Class][_skill].TDam+_damage;
			XBtnDps[Name][Class][_skill].Avg=XBar_Round(math.ceil((XBtnDps[Name][Class][_skill].TDam/XBtnDps[Name][Class][_skill].nHits)*100)/100,0);
			if (_damage>XBtnDps[Name][Class][_skill].BDam) then XBtnDps[Name][Class][_skill].BDam=_damage; end
		end
	end
--	Output
	local usrtxt={[1]=XBar["DpsV1"],[2]=XBar["DpsV2"]};
	local output="";
	for i=1,2 do
		if (usrtxt) and (usrtxt[i]~="") then
			usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[DPS%]",TempDps["DPS"]);
			usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[KILLS%]",(XBtnDps["kills"] or 0));
			usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[DMG%]",(_damage or 0));
		end
	end
	if (XBar["DpsT1"]==true) then output=usrtxt[1]; end
	if (XBar["DpsT2"]==true) then
		if (XBar["DpsT1"]==true) then output=output.."\n"..usrtxt[2]; else output=usrtxt[2]; end
	end
	XBarDps_F_Text:SetText(output);
--	EoL
end

function XBarDps_OnClick(this,key)
	if (key=="LBUTTON" and IsShiftKeyDown()) then XBar["EQDmg_X"]=-1; XBar["EQDmg_Y"]=-1; XBar["EQDmg_Scale"]=1; end
	if (key=="LBUTTON") then XBar_ToggleUIPanel(XBarEQDmg); end
end

--[[
function XBarDps_UpdateFrame()
	local a={}
	local THit,TMiss,TDdg,TCrit,TMissDdg=0,0,0,0,0
	local Ks_Skill,Ks_Dam,Ks_Crit,Ks_Avg="","","",""
	local Ks_ADam,Ks_MissHit,Ks_CritHit=0,0,0
	for n in pairs(XBtnDps[Name][Class]) do table.insert(a,n); end
	table.sort(a)
	for i,n in ipairs(a) do 
		if n=="ATTACK" then Ks_Skill=Ks_Skill.."\n".."Melee";
		else Ks_Skill=Ks_Skill.."\n"..n; end
		Ks_Dam=Ks_Dam.."\n"..XBtnDps[Name][Class][n].BDam
		Ks_Crit=Ks_Crit.."\n"..XBtnDps[Name][Class][n].BCrit
		Ks_Avg=Ks_Avg.."\n"..XBtnDps[Name][Class][n].Avg
		Ks_ADam=XBtnDps[Name][Class][n].Avg+Ks_ADam
		THit=THit+XBtnDps[Name][Class][n].nHits
		TDdg=TDdg+XBtnDps[Name][Class][n].nDodge
		TMiss=TMiss+XBtnDps[Name][Class][n].nMiss
		TCrit=TMiss+XBtnDps[Name][Class][n].nCrits
		TMissDdg=TMiss+TDdg
	end
		Ks_MissHit=XBar_Round(math.ceil((TMissDdg/THit)*100),2)
		Ks_CritHit=XBar_Round(math.ceil((TCrit/THit)*100),2)
	--New Peak DPS
	if (not TempDps["Crit"]) and (TempDps["DPS"]>TempDps["BestHit"]) then TempDps["BestHit"]=TempDps["DPS"]; end
	KISDM_DPS:SetText("DPS: |cffffffff"..TempDps["DPS"].."|r")
	KISDM_PDPS:SetText("M/H: |cffffffff"..Ks_MissHit.."%|r")
	KISDM_BDPS:SetText("C/H: |cffffffff"..Ks_CritHit.."%|r")
end
]]

